import {
  CHANGE_INPUT_VALUE,
  BUTTON_CLICK,
  DELETE_ITEM,
  INIT_ITEM,
  GET_INIT_LIST,
} from "../action";

const defaultState = {
  inputValue: "123",
  list: [1, 3],
};
export default (state = defaultState, action) => {
  if (action.type === CHANGE_INPUT_VALUE) {
    console.log(action);
    const newState = JSON.parse(JSON.stringify(state));
    newState.inputValue = action.value;
    return newState;
  }
  if (action.type === BUTTON_CLICK) {
    console.log(action);
    if (state.inputValue === "") return state;
    const newState = JSON.parse(JSON.stringify(state));
    newState.list.push(state.inputValue);
    newState.inputValue = "";
    console.log(newState);
    return newState;
  }
  if (action.type === DELETE_ITEM) {
    console.log(action);
    const newState = JSON.parse(JSON.stringify(state));
    newState.list.splice(action.index, 1);
    console.log(newState);
    return newState;
  }
  if (action.type === INIT_ITEM) {
    console.log(action);
    const newState = JSON.parse(JSON.stringify(state));
    newState.list = action.list;
    // let newState = {...state,action.list}
    console.log(newState);
    return newState;
  }
  return state;
};
